<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Entrance extends CI_Controller {

    //登录页面
	public function login()
	{
		$this->load->view('Entrance/login',$this->cap());
	}

	//验证码
	public function cap()
    {
        if($this->input->is_ajax_request()){
            //看不清楚 换一张
            $this->load->helper('captcha');
            $c = create_captcha();
            $this->ajax_return($c['url']);
        }
        $this->load->helper('captcha');
        return create_captcha();
    }

    //处理登录
    public function doLogin()
    {
        if($this->input->is_ajax_request()){

            $post = $this->input->post(['username','password','code'],true);

            $is_login = 0;
            if(strtolower($post['code']) !== $this->session->captcha_img){
                $is_login = 2;
            }else{
                $this->load->database();//加载数据库

                $userInfo = $this->db->query('select password from admin_user where account = "'.$post['username'].'"');
                $result = $userInfo->result_array();

                if($result && $result[0]['password'] == md6($post['password'])){
                    $is_login = 1;
                    $_SESSION['user_name'] = $post['username'];
                }
            }
            //log_message('debug', $is_login);
            $this->ajax_return($is_login);
        }
    }
}
